﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Business.DAO;
using Business.Models;
using Website.Models;

namespace Website.Controllers
{
    public class TruongHocController : Controller
    {
        TruongHocDAO dao = new TruongHocDAO();
        IEnumerable<TruongHoc> model = null;

        public ActionResult Index(int IdTruongHoc = 0, String Function = "")
        { 
            ViewBag.Function = Function;

            if (Function != "" && Function == "Delete" && IdTruongHoc != 0)
            {
                ViewBag.DeleteResult = dao.Delete(IdTruongHoc); 
                //Tra form lai dang Create Form
                ViewBag.Function = "Create";
            }

            model = dao.GetAll();
            return View(model);
        }

        [HttpPost]
        public ActionResult Index(TruongHoc truongHoc, String Function = "")
        {
            ViewBag.Function = Function;

            if (Function == "Edit")
            {
                Edit(truongHoc);
            }
            else if (Function == "Create")
            {
                Create(truongHoc); 
            }

            model = dao.GetAll();
            return View(model);
        }

        public ActionResult ManageQuanTri(int ID)
        {
            return View(new QuanTriDAO().GetQuanTri_Super(ID)); 
        }

        [HttpPost]
        public ActionResult ManageQuanTri(NhanVien nhanVien) 
        {
            NhanVienDAO dao = new NhanVienDAO();
            nhanVien.LoaiNhanVien = 0;
            nhanVien.NgayTao = DateTime.Now;


            if (nhanVien.IdNhanVien != 0)
            {
                //ViewBag.EditResult = dao.Edit(quanTri);
            }
            else
            {
                int idTruongHoc = 0;
                if (int.TryParse(Request.QueryString["ID"], out idTruongHoc))
                {
                    nhanVien.IdTruongHoc = idTruongHoc;
                }
                ViewBag.CreateResult = dao.CreateQuanTri(nhanVien, true);
            }
            return View();
        }

        public ActionResult Create(int IdDaiLy = 0)
        {
            return View();
        }

        private void Create(TruongHoc truongHoc)
        {
            try
            {
                truongHoc.NguoiTao = General.GetCurrentUserId();
                truongHoc.NgayTao = DateTime.Now;
                ViewBag.CreateResult = dao.Create(truongHoc);
            } 
            catch (Exception ex)
            {
            }
        }

        public ActionResult GridViewPartial()
        {
            try
            {
                model = dao.GetAll();
            }
            catch (Exception ex)
            {
            }
            return PartialView("GridView", model);
        }

        public ActionResult Edit(int IdTruongHoc) 
        {
            TruongHoc truongHoc = null;
            try
            {

                if (IdTruongHoc != 0)
                {
                    model = dao.GetAll();
                    truongHoc = model.Where(daily => daily.IdTruongHoc == IdTruongHoc).First();
                }
                else
                {
                    truongHoc = new TruongHoc();
                }
            }
            catch (Exception ex)
            {
            }
            return View(truongHoc);
        }

        private void Edit(TruongHoc truongHoc)
        {
            try
            {
                if (truongHoc.IdTruongHoc != 0)
                {
                    truongHoc.NguoiTao = General.GetCurrentUserId();
                    truongHoc.NgayTao = DateTime.Now;
                    ViewBag.EditResult = dao.Edit(truongHoc);
                }
            }
            catch (Exception ex)
            {
            }
        }

    }
}
